package com.kfm.am.tcpMany;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) throws IOException {
        System.out.println("客户端启动成功！");
        Socket socket = new Socket("localhost",8888);  // 与目标服务器（主机地址 + 端口号）进行连接
        OutputStream out = socket.getOutputStream();  // 获取连接的输出流，写东西到服务端、
        DataOutputStream dataOut = new DataOutputStream(out);

        InputStream inputStream = socket.getInputStream();
        DataInputStream in = new DataInputStream(inputStream);
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("你想说什么？");
            String msg = sc.next();
            try {
                dataOut.writeUTF(msg);
                System.out.println("发送消息成功");
                dataOut.flush();

                String s = in.readUTF();
                System.out.print("服务端回复你了：" + s);
            } catch(IOException e) {
                out.close();
                socket.close();
                break;
            }
        }
    }
}
